import nltk

nltk.usage(nltk.ClassifierI)

      

train = [(dict(a=1,b=1,c=1), 'y'),
         (dict(a=1,b=1,c=1), 'x'),
         (dict(a=1,b=1,c=0), 'y'),
         (dict(a=0,b=1,c=1), 'x'),
         (dict(a=0,b=1,c=1), 'y'),
         (dict(a=0,b=0,c=1), 'y'),
         (dict(a=0,b=1,c=0), 'x'),
         (dict(a=0,b=0,c=0), 'x'),
         (dict(a=0,b=1,c=1), 'y')]
         
test = [(dict(a=1,b=0,c=1)), # unseen
        (dict(a=1,b=0,c=0)), # unseen
        (dict(a=0,b=1,c=1)), # seen 3 times, labels=y,y,x
        (dict(a=0,b=1,c=0)), # seen 1 time, label=x
        ]

classifier = nltk.NaiveBayesClassifier.train(train)
print sorted(classifier.labels())

print classifier.batch_classify(test)


for pdist in classifier.batch_prob_classify(test):
    print '%.4f %.4f' % (pdist.prob('x'), pdist.prob('y'))
    

classifier.show_most_informative_features()

###################

train = [(dict(wyklad=1,sala=1,cv=0,muzyka=0), "announcement"),
         (dict(wyklad=0,sala=0,cv=1,muzyka=0), "job_announcement"),
         (dict(wyklad=0,sala=0,cv=0,muzyka=1), "unknown")]
         
test = [(dict(wyklad=1,sala=0,cv=1,muzyka=0)), # unseen
        (dict(wyklad=1,sala=0,cv=0,muzyka=0)), # unseen
        (dict(wyklad=0,sala=1,cv=0,muzyka=0)), # unseen
        (dict(wyklad=0,sala=0,cv=0,muzyka=0)),
        (dict(wyklad=0,sala=0,cv=0,muzyka=1)),
        ]

classifier = nltk.NaiveBayesClassifier.train(train)
print sorted(classifier.labels())

print classifier.batch_classify(test)

i = 0
for pdist in classifier.batch_prob_classify(test):
    print '%s %.4f %.4f %.4f' % (test[i], pdist.prob("job_announcement"), pdist.prob("announcement"), pdist.prob("unknown"))
    i += 1

classifier.show_most_informative_features()

################

from nltk.tokenize import *


expr = '(a b (c d)) e f (g)'

print SExprTokenizer().tokenize(expr)



